package property.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import property.pojo.*;
import property.service.QueryService;
import property.service.impl.QueryServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.List;


@WebServlet(urlPatterns = {"/search/*"})
public class QueryServlet extends BaseServlet {
    private QueryService queryService =new QueryServiceImpl();

    public void exampleQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");//获取json字符串
        JSONObject jb = JSONObject.parseObject(data);//解析json字符串
        Short findType = jb.getShort("findType");//获取查询类型（人or事务）
        JSONArray examplesArray = jb.getJSONArray("examples");//获取条件列表
        JSONObject jsonReturn = new JSONObject();//设置返回的json对象


        //在session的获取正在使用者
//        HttpSession session=request.getSession();
//        Object currentUser = session.getAttribute("currentUser");
//        System.out.println(session);//验证是否为同一个session;
//        Object javaQCS = qcs.toJavaObject(Object.class);
        //进行查询
        List list= queryService.peopleAndThingQuery(findType,examplesArray);

        jsonReturn.put("code",1);
        jsonReturn.put("findType",findType);
        jsonReturn.put("result",list);
        jsonReturn.put("messages","查询成功");
        response.getWriter().write(jsonReturn.toString());
    }

    public void peopleLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);//将json字符串转为json对象
        JSONObject jsonReturn = new JSONObject();//设置返回的json对象

        //获取账号密码
        String phone = jb.getString("phone");
        String password = jb.getString("password");

        //0 -- 用户  1 -- 员工  2 -- 管理员
        short loginType= jb.getShort("loginType");
        if(phone.isEmpty()||password.isEmpty()){
            //判断账号密码是否存在，存在则进行验证正确,不存在则返回
            System.out.println("账号或密码为空");

            jsonReturn.put("code",0);
            jsonReturn.put("messages","账号或密码为空");
            response.getWriter().write(jsonReturn.toString());
            return;
        }

        //接收登录成功的实体对象
        List list=new ArrayList();
        if (loginType==0){
            //用户登录
            UserExample userExample=new UserExample();
            UserExample.Criteria userExampleCriteria = userExample.createCriteria();
            userExampleCriteria.andUPasswordEqualTo(password).andUPhoneEqualTo(phone);
            list = queryService.peopleLogin(loginType,userExample);

        }else if(loginType==1){
            //员工登录
            Staff staff=new Staff();
            StaffExample staffExample=new StaffExample();
            StaffExample.Criteria staffExampleCriteria = staffExample.createCriteria();
            staffExampleCriteria.andSPasswordEqualTo(password).andSPhoneEqualTo(phone);
            list = queryService.peopleLogin(loginType,staffExample);
        }else if(loginType==2){
            //管理员登录
            Admin admin=new Admin();
            AdminExample adminExample=new AdminExample();
            AdminExample.Criteria adminExampleCriteria = adminExample.createCriteria();
            adminExampleCriteria.andAPasswordEqualTo(password).andAPhoneEqualTo(phone);
            list = queryService.peopleLogin(loginType,adminExample);
        }
        if(list.isEmpty()){
            System.out.println("账号或密码错误");

            jsonReturn.put("code",0);
            jsonReturn.put("messages","账号或密码错误");
            response.getWriter().write(jsonReturn.toString());

        }else {
            jsonReturn.put("code",1);
            jsonReturn.put("messages","登录成功");
            // 为正在登录的对象创建会话，以便后续调用
            HttpSession session = request.getSession();
            session.setAttribute("loginType",loginType);
            session.setAttribute("currentUser",list.get(0));

            jsonReturn.put("redirect","/indexHtml");
            response.getWriter().write(jsonReturn.toString());
        }
    }

}
